<template>
    <el-dialog :title="title" :visible.sync="classifyOpen" width="60%" :before-close="closeDelDialog">
        <div>
            <el-form :model="queryAddParams" ref="queryAddForm" :inline="true" v-show="showSearch" label-width="68px">
                <el-form-item label="显示名称" prop="fieldCaption">
                    <el-input v-model="queryAddParams.fieldCaption" placeholder="请输入显示名称" clearable style="width: 240px"
                        @keyup.enter.native="addHandleQuery" />
                </el-form-item>
                <el-form-item label="字段名称" prop="fieldName">
                    <el-input v-model="queryAddParams.fieldName" placeholder="请输入字段名称" clearable style="width: 240px" @input="toUpperCase"
                        @keyup.enter.native="addHandleQuery" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" @click="addHandleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="addResetQuery">重置</el-button>
                </el-form-item>
            </el-form>
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-select v-model="queryAddParams.type" style="width:140px" @change="cateMdataSelectTap($event)">
                        <el-option v-for="(dict, index) in deptOptionsRowType" :key="index" :label="dict.label"
                            :value="dict.value" />
                    </el-select>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" @click="onAddTap">添加</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="danger" plain icon="el-icon-delete" @click="onDeleteTap">删除</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getlist"></right-toolbar>
            </el-row>
            <el-table v-loading="loading" :data="addList" height="480" border @selection-change="addSelectionChange">
                <el-table-column type="selection" width="50" align="center" />
                <el-table-column label="序号" align="center" type="index" />
                <el-table-column label="显示名称" width="150" align="center" key="FIELD_CAPTION" prop="FIELD_CAPTION">
                </el-table-column>
                <el-table-column label="字段名称" align="center" key="FIELD_NAME" prop="FIELD_NAME"
                    :show-overflow-tooltip="true" />
                <el-table-column label="字段类型" align="center" key="fieldType" prop="fieldType" :show-overflow-tooltip="true">
                    <template slot-scope="scope">
                        <span v-for="dict in basic_data_fieldtypeList" :key="dict.dictValue">{{ scope.row.FIELDTYPE ==
                            dict.dictValue ? dict.dictLabel : '' }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="是否分类" align="center" key="isCommon" prop="isCommon" :show-overflow-tooltip="true">
                    <template slot-scope="scope">
                        {{ scope.row.HAD == 1 ? "已分类" : "未分类" }}
                    </template>
                </el-table-column>

            </el-table>

            <pagination v-show="addTotal > 0" :total="addTotal" :page.sync="queryAddParams.pageNum"
                :limit.sync="queryAddParams.pageSize" @pagination="getlist" />
        </div>
        <!-- <span slot="footer" class="dialog-footer">
            <el-button @click="closeDelDialog">取 消</el-button>
            <el-button type="primary" @click="addSubmitTap">保 存</el-button>
        </span> -->
    </el-dialog>
</template>
<script>
import { listforshy, addmdatatoshy,deletemdatafromshy} from "@/api/archive/archivesCategory.js";
import { listforshy2, addmdatatoshy2,deletemdatafromshy2 } from "@/api/manage/archivesCategory.js";
export default {
    dicts: ['sys_normal_disable', 'basic_data_fieldtype'],
    // cateIsType  0=应用定义档案门类里面的元数据方案明细列表添加    1=应用定义里面的元数据方案明细列表添加  2=manager下面的模板管理的元数据方案添加
    watch: {
        addParams: {
            deep: true,
            immediate: true, // 监听到后，立即执行 handler方法
            handler: function (newVal, oldVal) {
                this.queryAddParams.groupId = newVal.queryAddParams.groupId;
                this.queryAddParams.type = newVal.queryAddParams.type;
                this.queryAddParams.categoryId = newVal.queryAddParams.categoryId;
                this.cateIsType = newVal.cateIsType;
                this.getlist();
                this.getDictsInfo();

            },

        }
    },
    props: ['classifyOpen', 'deptOptionsRowType', 'addParams'],
    data() {
        return {
            title: "分类方法",
            addList: [],
            // 遮罩层
            loading: true,
            cateIsType: "",
            //元数据方案明细查询参数
            queryAddParams: {
                pageNum: 1,
                pageSize: 10,
                groupId: undefined,
                fieldCaption: "",
                fieldName: "",
                type: undefined,
            },
            basic_data_fieldtypeList: [],
            // 显示搜索条件
            showSearch: true,
            //添加的列表数据  总条数
            addTotal: 0,
            //添加列表的选中数组
            Ids: [],
            // 非多个禁用
            multiple: true,
        }
    },

    methods: {
        getDictsInfo() {
            if (this.cateIsType == "0" || this.cateIsType == "1") {
                this.getDicts_archive("BASIC_DATA_FIELDTYPE").then(res => {
                    this.basic_data_fieldtypeList = res.data;

                })
            } else {
                this.getDicts_user("BASIC_DATA_FIELDTYPE").then(res => {
                    this.basic_data_fieldtypeList = res.data;

                })


            }



        },
        //数据列表
        getlist() {
            if (this.cateIsType == "0" || this.cateIsType == "1") {

                listforshy(this.queryAddParams).then(response => {
                    this.addList = response.rows;
                    this.addTotal = response.total;
                    this.loading = false;
                });
            } else {

                listforshy2(this.queryAddParams).then(response => {
                    this.addList = response.rows;
                    this.addTotal = response.total;
                    this.loading = false;
                });

            }



        },
        // 添加多选框选中数据
        addSelectionChange(selection) {
            this.Ids = selection.map(item => item.ID)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },

        //重置
        addResetQuery() {
            this.$refs.queryAddForm.resetFields();
            this.getlist();
        },

        toUpperCase() {
            this.queryAddParams.fieldName = this.queryAddParams.fieldName.toUpperCase()
        },

        //搜索
        addHandleQuery() {
            this.getlist()
        },


        closeDelDialog() {
            this.$emit("closeClassifyDialog")
        },
        //元数据信息明细类型触发事件
        cateMdataSelectTap(data) {
            console.log(data)
            this.queryAddParams.type = data;
            console.log(this.queryAddParams, "++++++++++++++")
            this.getlist();
        },

        //把元数据添加到三合一表
        onAddTap() {
            var that = this;
           // console.log(this.single, this.multiple, this.cateIsType == "0" || this.cateIsType == "1")
            if (!this.multiple) {
                if (this.cateIsType == "0" || this.cateIsType == "1") {
                    addmdatatoshy({
                        ids: this.Ids,
                        type: this.queryAddParams.type,
                        categoryId: this.queryAddParams.categoryId,
                        groupId: this.queryAddParams.groupId
                    }).then(res => {
                        console.log(res, "000000000000000000000")
                        if (res.code == "200") {
                            this.$modal.msgSuccess("修改成功");

                            that.getlist()
                        }

                    }).catch(res => {

                    })
                } else {
                    addmdatatoshy2({
                        ids: this.Ids,
                        type: this.queryAddParams.type,
                        categoryId: this.queryAddParams.categoryId,
                        groupId: this.queryAddParams.groupId
                    }).then(res => {
                        console.log(res, "000000000000000000000")
                        if (res.code == "200") {
                            this.$modal.msgSuccess("修改成功");

                            that.getlist()
                        }

                    }).catch(res => {

                    })
                }
            } else {
                this.$modal.msgWarning('请选择先选择要添加的数据哦')
            }
        },
        onDeleteTap() {
            var that=this;
            // console.log(this.single, this.multiple, this.cateIsType ,"--------------------------------------"+this.cateIsType == "0" || this.cateIsType == "1")
            if (!this.multiple) {
                if (this.cateIsType == "0" || this.cateIsType == "1") {
                    deletemdatafromshy({
                        ids: this.Ids,
                        type: this.queryAddParams.type,
                        categoryId: this.queryAddParams.categoryId,
                        groupId: this.queryAddParams.groupId
                    }).then(res => {
                        console.log(res, "000000000000000000000")
                        if (res.code == "200") {
                            this.$modal.msgSuccess("删除成功");

                            that.getlist()
                        }else{
                            this.$modal.msgSuccess("删除失败");
                        }

                    }).catch(res => {

                    })
                } else {
                    console.log("11111111111111111111111111111111")
                    deletemdatafromshy2({
                        ids: this.Ids,
                        type: this.queryAddParams.type,
                        categoryId: this.queryAddParams.categoryId,
                        groupId: this.queryAddParams.groupId
                    }).then(res => {
                        console.log(res, "000000000000000000000")
                        if (res.code == "200") {
                            this.$modal.msgSuccess("删除成功");

                             that.getlist()
                        }else{
                            this.$modal.msgSuccess("删除失败");
                        }

                    }).catch(res => {

                    })
                }
            } else {
                this.$modal.msgWarning('请选择先选择要删除的数据哦')
            }
        },

    }
}

</script>
<style lang="scss" scoped>
::v-deep .el-dialog {
    display: flex;
    flex-direction: column;
    margin: 0 !important;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    max-height: calc(100% - 30px);
    max-width: calc(100% - 30px);
}

::v-deep .el-dialog .el-dialog__body {
    flex: 1;
    overflow: auto;
}
</style>

